from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtWidgets import QStyledItemDelegate, QSpinBox


class Integer(QStyledItemDelegate):
    def __init__(self, parent, constraints):
        super().__init__(parent)
        self.constraints = constraints

    def createEditor(self, parent, option, index):
        spin = QSpinBox(parent)
        spin.setFrame(False)

        return spin
        

    def setEditorData(self, spin, index):
        value = index.model().data(index, Qt.EditRole)
        if(value==''):
            spin.setValue(0)
        else:
            spin.setValue(int(value))


    def setModelData(self, spin, model, index):
        spin.interpretText()
        model.setData(index, spin.value(), Qt.EditRole)


    def updateEditorGeometry(self, spin, option, index):
        spin.setGeometry(option.rect)
